<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

class SessionsController extends Controller
{
    protected $redirectTo = '/';
    //
    public function creat(Request $request){

        $this->validate($request, [
            'captcha' => 'required|captcha',
        ],[
            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '请输入正确的验证码',
        ]);
        $name = $request['u_name'];
        $password = $request['password'];
        if (Auth::attempt(['u_name' => $name, 'password' => $password],$request->has('remember'))){
            return redirect()->intended(route('users.show',[Auth::user()]));
        }else if (Auth::attempt(['mobile' => $name, 'password' => $password],$request->has('remember'))){
            return redirect()->intended(route('users.show',[Auth::user()]));
        }else
            return redirect()->route('users.login')->with('msg','用户名或密码错误！');
    }
    public function destroy(){
        Auth::logout();
        session()->flash('success','退出成功！');
        return redirect()->route('users.login');
    }
}
